package com.roc.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class Constance {
	public final static Logger logger = LogManager.getLogger(Constance.class);
	public final static String ANALY_ADMIN="admin";//数据分析系统添加相关菜单所用的当前操作员
	public static final String DATA_FORMAT_PATTERN_01 = "yyyy-MM-dd HH:mm:ss";
	public static final String DATA_FORMAT_PATTERN_02 = "yyyy-MM-dd";
	public static final String DATA_FORMAT_PATTERN_03 = "yyyyMMddHHmmss";
	public static final String DATA_FORMAT_PATTERN_04 = "yyyyMMdd";
	public static final String DATA_FORMAT_PATTERN_05 = "yyyy-MM-dd HH:mm:ss SSS";
	public final static String MERCHANT_TYPE_SYSTEM = "00";// 系统管理员
	public final static String MERCHANT_TYPE_ADMIN = "01";// 管理员
	public final static String MERCHANT_TYPE_ODAN = "02";// O单管理员
	public final static String MERCHANT_TYPE_MER = "15";// 特约商户
	public final static String MERCHANT_TYPE_DAILI = "01";// 代理
	public final static String MERCHANT_TYPE_RECOMANFER = "02";// 推荐人

	public final static String MERCHANT_TYPE_MARKET = "03";// 市场人员

	public final static String MERCHANT_TYPE_CAIWU = "04";// 财务
	public final static String MERCHANT_TYPE_STORE = "05";// 库存
	// 商户状态
	public final static long MERCHANT_FLAG_ADD = 0;// 添加
	public final static long MERCHANT_FLAG_ONE = 1;// 添加
	public final static long MERCHANT_FLAG_TWO = 2;// 添加
	public final static long MERCHANT_FLAG_THREE = 3;// 添加
	// 商户号前三位默认值
	public final static String MERCHANT_NO = "000";// 正常
	public final static String MERCHANT_NO_LAST = "0001";// 商户号最后序列
	// 代理商状态
	public final static String AGENT_STATUS_TYPE0 = "00";// 正常
	public final static String AGENT_STATUS_TYPE1 = "01";// 待审核
	public final static String AGENT_STATUS_TYPE2 = "02"; // 注销
	// 添加的终端数量
	public final static String TERM_APPLY_STATUS_W = "W"; // 待审核
	public final static String TERM_APPLY_STATUS_F = "F"; // 审核成功

	public final static String OPERATORS_FLAG_OEM = "1";// O单品牌
	public final static String OPERATORS_FLAG_SELF = "0";// 直营

	// public final static int PAGE_MAX_SIZE_100=100;
	public final static int PAGE_MAX_SIZE = 20;

	public final static int USER_PAGE_SIZE = 20;

	public final static int POS_SN_MAX_SIZE = 1000;

	// 云商宝 页面查询条件  SN 号段最大长度
	public final static int YSB_POS_SN_SELECT_MAX_SIZE = 100;

	public static final String[] HQLForbiddenStr = new String[] { "'" };

	public static String formatDate(String formater) {
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
		String date = sdf.format(new Date(System.currentTimeMillis()));
		return date;
	}

	public static String trim(String obj) {
		if (obj == null) {
			return "";
		}
		return obj.trim();
	}

	public static String isMoney(String obj) {
		if (obj == null) {
			return "0.00";
		}
		double money = Double.parseDouble(obj) / 100;

		return Double.toString(money);
	}

	public static boolean isNotEmpty(String param) {
		if (param == null || param.trim().length() == 0) {
			return false;
		}
		return true;
	}

	public static boolean isEmpty(String param) {
		if (param == null || param.trim().length() == 0) {
			return true;
		}
		return false;
	}

	public static String getArrayContent(String[] str) {
		String str2 = "";
		String str3 = "";
		for (int i = 0; i < str.length; i++) {
			String str1 = str[i];
			if (i != str.length - 1) {
				str2 = str1 + ",";
			} else {
				str2 = str1;
			}

			str3 = str3 + str2;// 1, 2,

		}
		return str3;
	}

	public static String getObjArrayContent(Object[] str) {
		String str2 = "";
		String str3 = "";
		for (int i = 0; i < str.length; i++) {
			String str1 = String.valueOf(str[i]);
			if (i != str.length - 1) {
				str2 = str1 + ",";
			} else {
				str2 = str1;
			}
			str3 = str3 + str2;// 1, 2,
		}
		return str3;
	}

	public static String getArrayStringContent(String[] str) {
		String str2 = "";
		String str3 = "";
		for (int i = 0; i < str.length; i++) {
			String str1 = str[i];
			if (i != str.length - 1) {
				str2 = "'" + str1 + "',";
			} else {
				str2 = "'" + str1 + "'";
			}

			str3 = str3 + str2;// 1, 2,

		}
		return str3;
	}

	public static String numberFormat(double obj) {
		DecimalFormat myFormatter = new DecimalFormat("##########.##");

		return myFormatter.format(obj);

	}

	// 处理商户号序列
	public static String handleStr(long a) {
		String b = Long.toString(a);

		if (b.length() == 1) {
			b = "000" + b;
		} else if (b.length() == 2) {
			b = "00" + b;
		} else if (b.length() == 3) {
			b = "0" + b;
		}

		return b;
	}

	// 生成15位临时商户号
	public synchronized static String isMid() {
		String mid_12 = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
		Random ra = new Random();
		int mid_3 = 100 + ra.nextInt(901);
		return mid_12 + Integer.toString(mid_3);
	}

	// 生成八位随机的终端号
	public static String isTid() {
		Random ra = new Random();
		Long mid_3 = Long.parseLong("10000000") + ra.nextInt(89999999);
		return mid_3.toString();
	}

	// 生成八位随机数，防止上个方法改规则
	public static String generateRanom() {
		Random ra = new Random();
		Long mid_3 = Long.parseLong("10000000") + ra.nextInt(89999999);
		return mid_3.toString();
	}

	public static String Null2EmptyTrim(String str) {
		if (str == null || "".equals(str)) {
			return "";
		}
		return str.trim();
	}

	public static String Null2EmptyTrim(Object obj) {
		if (obj == null) {
			return "";
		}
		String str = String.valueOf(obj);
		if (str == null || "".equals(str)) {
			return "";
		}
		return str.trim();
	}

	public static String filterHQL(String orgStr) {
		for (int i = 0; i < HQLForbiddenStr.length; i++) {
			orgStr.replaceAll(HQLForbiddenStr[i], "\'" + HQLForbiddenStr[i]);
			// orgStr = String.replace(orgStr, HQLForbiddenStr[i], "\'" +
			// HQLForbiddenStr[i]);
		}
		return orgStr;
	}

	public static String join(Object[] objVal, String joinFlag) {
		StringBuffer sb = new StringBuffer();
		if (objVal != null && objVal.length > 0) {
			for (Object obj : objVal) {
				sb.append(joinFlag).append(obj);
			}
			return sb.toString().substring(1);
		}

		return null;
	}

	public static java.sql.Date formateDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.sql.Date temp = null;
		try {
			temp = new java.sql.Date(sdf.parse(date).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}

	public static java.sql.Date formateStartDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.sql.Date temp = null;
		try {
			temp = new java.sql.Date(sdf.parse(date + " 00:00:00").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}

	public static java.sql.Date formateEndDate(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.sql.Date temp = null;
		try {
			temp = new java.sql.Date(sdf.parse(date + " 23:59:59").getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}

	public static String list2SqlStr(List<String> list) {
		StringBuffer sb = new StringBuffer();
		for (String obj : list) {
			sb.append(",'").append(Null2EmptyTrim(obj)).append("'");
		}
		return sb.substring(1);
	}

	// 将页面上的两个日期判断
	public static boolean theDateIsTrue(String startDate, String endDate) {
		String start = startDate.substring(4, 6).trim();
		String end = endDate.substring(4, 6).trim();
		if (!start.equals(end)) {
			return false;
		} else {
			return true;
		}

	}

	public static String formateDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		return sdf.format(date);
	}

	public static String returnCurrentYear() {
		String year = new SimpleDateFormat("YYYY").format(new Date());
		return year;
	}

	public static String returnCurrentMonth() {
		String month = new SimpleDateFormat("MM").format(new Date());
		return month;
	}

	public static String formateDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		String temp = sdf.format(new Date());

		return temp;
	}

	public static String formateDate1() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

		String temp = sdf.format(new Date());

		return temp;
	}

	public static String formateDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		String temp = sdf.format(new Date());

		return temp;
	}

	public static void main(String[] args) {

		// log.info("tetetetet");
		// System.out.println(calcuDate("20131118"));

		System.out.println(formateDate1());
	}

	public static String calcuDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String newDate = null;
		try {
			Date date = sdf.parse(dateStr);
			Date date2 = new Date(date.getTime() - 24 * 60 * 60 * 1000);
			newDate = sdf.format(date2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newDate;
	}

	public static String initTermSerial() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
		String temp = sdf.format(new Date());
		return temp;
	}

	public static String currTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String temp = sdf.format(new Date());
		return temp;
	}

	public static String currTime1() {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		String temp = sdf.format(new Date());
		return temp;
	}

	public static String fillTo4(int a) {
		if (a >= 0 && a < 9) {
			return "000" + a;
		} else if (a >= 10 && a < 99) {
			return "00" + a;
		} else if (a >= 100 && a < 999) {
			return "0" + a;
		} else if (a >= 1000 && a < 9999) {
			return "" + a;
		} else {
			return "" + a;
		}

	}

	public static String fillTo8(int obj, int totalLen) {
		String temp = String.valueOf(obj);
		int len = temp.length();
		String str = "";
		for (int i = 0; i < totalLen - len; i++) {
			str += "0";
		}
		str += temp;
		// int youNumber = 1;
		// // 0 代表前面补充0
		// // 4 代表长度为4
		// // d 代表参数为正数型
		// String strs = String.format("%08d", youNumber);
		// System.out.println(strs); // 00000001

		return str;
	}

	public static String simpleDateTimeFormat(String dateTime) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df.parse(dateTime);
		return time.format(date);
	}

	public static String null2Amt(Object obj) {
		if (obj == null) {
			return "0.00";
		}
		String amt = String.valueOf(obj).trim();
		if ("".equals(amt)) {
			return "0.00";
		}
		return amt;
	}

	public static String null2Zero(Object obj) {
		if (obj == null) {
			return "0";
		}

		String str = String.valueOf(obj).trim();
		if ("".equals(str)) {
			return "0";
		}
		return str;

	}

	/**
	 * 生成SN批次号,日期_随机数
	 * @return 批次号
	 */
	public static String getSnBatchNo() {
		Random r = new Random();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSSS");
		String batchno = sdf.format(new Date()) + "_" + r.nextInt(100000);
		return batchno;
	}

	/**
	 * 取 SN 中 最后一个 字符的位置
	 * @param s SN ,列如:CS-0001,返回 - 的位置, CS-A0001,返回 A 的位置
	 * @return 最后一个 字符的位置
	 */
	public static int getSNLastCharPlace(String s) {
		int count = -1;
		for (int i = s.length() - 1; i >= 0; i--) {
			char c = s.charAt(i);
			// 字符为英文或_,则返回 此位置 ( 'A' < c && c < 'z' )
			if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || ('-' == c)) {
				// System.out.println(i);
				count = i;
				break;
			}
		}
		return count;
	}

	public static java.sql.Date formateDateTime(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.sql.Date temp = null;
		try {
			temp = new java.sql.Date(sdf.parse(date).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return temp;
	}

	public static String randomNums(int length) {
		String code = "";
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			code = code + String.valueOf(r.nextInt(10));
		}
		return code;
	}

	public static String obtainYesterday() {
		SimpleDateFormat dft = new SimpleDateFormat(DATA_FORMAT_PATTERN_04);
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
		return dft.format(date.getTime());
	}

	public static BigDecimal Null2Zero(Object obj) {
		BigDecimal num = new BigDecimal(0);
		if (obj == null) {
			return num;
		}
		String str = String.valueOf(obj);
		if (str == null || "".equals(str)) {
			return num;
		}
		return new BigDecimal(obj.toString());
	}
}
